
import 'dart:convert';
import 'dart:io';

import 'package:music_player/bean/music_bean.dart';
import 'package:music_player/model/musci_model.dart';
import 'package:flutter/cupertino.dart';
import 'package:provider/provider.dart';

// 初始化音乐列表
void initMusicList (BuildContext context) async{
  final file = File("D:/.music");
  if(!file.existsSync()){
    debugPrint("文件不存在，使用默认的音乐列表");
    return;
  }

  final list  = file.readAsLinesSync();
  final musicModel = context.read<MusicModel>();
  musicModel.musicList.clear();

  List<MusicBean> ml =[];
  for (var s in list) {
    dynamic json;
    try {
      json = jsonDecode(s);
    } catch (e) {
      debugPrint("添加列表失败");
      continue;
    }
    ml.add(MusicBean.fromJson(json));
  }
  musicModel.musicList = ml;

}